package com.bluevally.microservice.user.bindable;

import com.bluevally.framework.client.context.AutoConnection;
import com.bluevally.grpc.user.UserProto;
import com.bluevally.grpc.user.UserServiceGrpc.UserServiceImplBase;
import com.bluevally.microservice.user.dao.CommonDao;
import com.bluevally.microservice.user.entity.UserEntity;
import io.grpc.stub.StreamObserver;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Created by lixiaoming on 2019/1/9.
 */
public class UserServiceImpl extends UserServiceImplBase {

    private static Log logger = LogFactory.getLog(UserServiceImpl.class);

    @Override
    @AutoConnection
    public void register(UserProto.UserRegRequest request, StreamObserver<UserProto.UserReponse> responseObserver) {
        //super.register(request, responseObserver);
        test();
        String name = request.getName();
        String passwd = request.getPasswd();
        UserEntity condition = new UserEntity();
        condition.setUserName(name);
        try {
            UserEntity exist = CommonDao.queryRecord(condition);
            if(exist==null){
                UserEntity newRecord = new UserEntity();
                newRecord.setUserName(name);
                newRecord.setPassWord(passwd);
                CommonDao.dbNewRecord(newRecord);


                final UserProto.UserReponse.Builder builder = UserProto.UserReponse.newBuilder();
                builder.setRetCode("000000");
                builder.setMessage("success");
                responseObserver.onNext(builder.build());
                responseObserver.onCompleted();
            }else{
                final UserProto.UserReponse.Builder builder = UserProto.UserReponse.newBuilder();
                builder.setRetCode("999999");
                builder.setMessage("exist user!");
                responseObserver.onNext(builder.build());
                responseObserver.onCompleted();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void test(){
        System.out.println("TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST");
    }
}
